from blls.img_blls.img_bll import ImgBLL
from handlers.base_handler import BaseHandler
from utils.check_params_utlil import CheckParams
from utils.handler_util import HandlerUtil


class ImgDownloadHandler(BaseHandler):
    def post(self):
        check_r = CheckParams(params=self.request_data,
                              conditions=[["date", True, ""]
                                          # ["local_path", True,""]
                                          ]).check()
        if not check_r.is_success:
            return self.write(HandlerUtil.set_failure_response(message=check_r.message))
        res = ImgBLL(request=self).download_img(check_r.data)
        return self.write(HandlerUtil.set_response(data=res.data,message=res.message, is_success=res.is_success))

class DownloadProgressHandler(BaseHandler):
    def post(self):
        check_r = CheckParams(params=self.request_data,
                              conditions=[
                                          ]).check()
        if not check_r.is_success:
            return self.write(HandlerUtil.set_failure_response(message=check_r.message))
        res = ImgBLL(request=self).download_progress()
        return self.write(HandlerUtil.set_response(data=res.data,message=res.message, is_success=res.is_success))


class UpdateResultHandler(BaseHandler):
    # @authenticated
    async def post(self):
        required_params = [
            ["project_id", False, ""]
        ]
        res_data = CheckParams(params=self.request_data, conditions=required_params).check()
        if not res_data.is_success:
            return self.write(HandlerUtil.set_failure_response(message=res_data.message))
        res = ImgBLL(request=self).updata_result(**res_data.data)
        return self.write(HandlerUtil.set_response(data=res.data, message=res.message, is_success=res.is_success))


class ImgUploadHandler(BaseHandler):
    def post(self):
        for key in self.request.arguments:
            self.request_data[key] = self.get_argument(key)
        print(self.request_data)
        check = CheckParams(params=self.request_data,
                            conditions=[["timestamp", True, ""],
                                        ["sign", True, ""],
                                        ]).check()
        if not check.is_success:
            return self.write(HandlerUtil.set_failure_response(message=check.message))
        files = self.request.files
        res = ImgBLL(request=self).upload_img(files, check.data)
        return self.write(HandlerUtil.set_response(data=res.data, message=res.message, is_success=res.is_success))


class TestDataHandler(BaseHandler):
    def post(self):
        check_r = CheckParams(params=self.request_data,
                              conditions=[["date", False, ""]
                                          ]).check()
        if not check_r.is_success:
            return self.write(HandlerUtil.set_failure_response(message=check_r.message))
        res = ImgBLL(request=self).data_result(check_r.data)
        return self.write(HandlerUtil.set_response(data=res.data,message=res.message, is_success=res.is_success))